<?php

class File extends CActiveRecord {
    const TABLE_NAME = 'file';
    public $sid;    // Transient session-id

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }
    public function tableName() {
        // Auto create table if it's not created
        $conn = Yii::app()->db;
        $file = new File;
        if ($conn->createCommand("SHOW TABLES LIKE '".self::TABLE_NAME."'")->query()->rowCount <= 0) {
            $sql = file_get_contents(dirname(__FILE__)."/schema.sql");
            $conn->createCommand($sql)->execute();
        }
        return self::TABLE_NAME;
    }

    public function rules() {
        return array(
                array('fid, uid', 'safe'),
                array('entity','length','max'=>255),
                array('filename','length','max'=>255),
                array('filemime','length','max'=>255),
                array('EXid, filesize, weight, status, timestamp', 'numerical', 'integerOnly'=>true),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'fid'           => Yii::t('amo','File id'),
                'entity'        => Yii::t('amo','Entity'),
                'EXid'          => Yii::t('amo','Entity id'),
                'uid'           => Yii::t('amo','User id'),
                'weight'        => Yii::t('amo','Weight'),
                'status'        => Yii::t('amo','Status'),
                'timestamp'     => Yii::t('amo','Timestamp'),
                'filename'      => Yii::t('amo','File name'),
                'filemime'      => Yii::t('amo','File mime'),
                'filesize'      => Yii::t('amo','File size'),
        );
    }

    public function toArray() {
        return array(
                'fid'       => $this->fid,
                'sid'       => $this->sid,
                'entity'    => $this->entity,
                'EXid'      => $this->EXid,
                'uid'       => $this->uid,
                'weight'    => $this->weight,
                'status'    => $this->status,
                'timestamp' => $this->timestamp,
                'filename'  => $this->filename,
                'filemime'  => $this->filemime,
                'filesize'  => $this->filesize,
        );
    }
    public static function buildFromArray($array) {
        $file = new File();
        $file->fid          = isset($array['fid'])? $array['fid']: 0;
        $file->sid          = isset($array['sid'])? $array['sid']: null; // declared as atr
        $file->entity       = isset($array['entity'])? $array['entity']: null;
        $file->EXid         = isset($array['EXid'])? $array['EXid']: 0;
        $file->uid          = isset($array['uid'])? $array['uid']: '';
        $file->weight       = isset($array['weight'])? $array['weight']: 0;
        $file->status       = isset($array['status'])? $array['status']: 0;
        $file->timestamp    = isset($array['timestamp'])? $array['timestamp']: 0;
        $file->filename     = isset($array['filename'])? $array['filename']: '';
        $file->filemime     = isset($array['filemime'])? $array['filemime']: '';
        $file->filesize     = isset($array['filesize'])? $array['filesize']: 0;
        return $file;
    }

    public function __toString() {
        return 'file('.$this->fid.','.$this->entity.','.$this->EXid.','.
                $this->uid.','.$this->weight.','.$this->status.','.$this->filename.','.
                $this->filemime.','.$this->filesize.')';
    }
}